import 'dart:convert';

import 'package:demo/pages.dart';
import 'package:demo/resources.dart';
import 'package:demo/routers.dart';
import 'package:demo/system_ui_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import 'loading_dialog.dart';
import 'navigator_observer.dart';

void main() {
  Routers.initRouters();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      navigatorKey: Routers.navigatorKey,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      supportedLocales: const [Locale('zh'), Locale('CN')],
      locale: const Locale('zh', 'CN'),
      theme: ThemeData(
          brightness: Brightness.light,
          primaryColor: Resources.colorPrimary,
          primarySwatch: _colorPrimary,
          textButtonTheme: TextButtonThemeData(
              style: TextButton.styleFrom(
                  minimumSize: const Size(1, 1),
                  padding: EdgeInsets.zero,
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8))))),
          buttonTheme: const ButtonThemeData(
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
              padding: EdgeInsets.zero,
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
          iconButtonTheme: IconButtonThemeData(
              style: IconButton.styleFrom(
                  minimumSize: const Size(1, 1),
                  padding: EdgeInsets.zero,
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8)))))),
      onGenerateRoute: Routers.router.generator,
      navigatorObservers: [RouterNavigatorObserver(), FlutterSmartDialog.observer],
      builder: FlutterSmartDialog.init(
          builder: (context, widget) {
            return MediaQuery(
                data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), //设置文字大小不随系统设置改变
                child: widget!);
          },
          loadingBuilder: (String msg) => LoadingDialog(msg)),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }

  final MaterialColor _colorPrimary = const MaterialColor(
    Resources.colorPrimaryValue,
    <int, Color>{
      50: Color(0xFFFFD58F),
      100: Color(0xFFFFD185),
      200: Color(0xFFFFCA71),
      300: Color(0xFFFFC25D),
      400: Color(0xFFFFBB48),
      500: Color(Resources.colorPrimaryValue),
      600: Color(0xFFE5A12F),
      700: Color(0xFFCC8F2A),
      800: Color(0xFFB27D24),
      900: Color(0xFF996B1F),
    },
  );
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text(widget.title)),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const SizedBox(height: 20),
              TextButton(
                  onPressed: () async {
                    Routers.push(context, Pages.content);
                  },
                  style: TextButton.styleFrom(backgroundColor: Colors.amber, minimumSize: const Size(120, 40)),
                  child: const Text('内容填充状态栏', style: TextStyle(color: Colors.white))),
              const SizedBox(height: 20),
              TextButton(
                  onPressed: () {
                    Routers.push(context, Pages.player);
                    // Routers.push(context, Pages.browser, arguments: {"title": "键盘挡住输入框", "url": "https://h5.dailyworkout.cn/qyCustomerServiceClient/index.html?need_bar=1&uid=6000000000&sid=d3637788142f58e7afe0a247ce3648da&nickName=百吉猫&phone=153****9065&version=ft-1.22.3&channel=1000001&device_version=Android 14&ministerial_is_not_empty=false&is_pad=false"});
                  },
                  style: TextButton.styleFrom(backgroundColor: Colors.amber, minimumSize: const Size(120, 40)),
                  child: const Text('视频播放', style: TextStyle(color: Colors.white))),
              const SizedBox(height: 20),
              Row(mainAxisSize: MainAxisSize.min, children: [
                TextButton(
                    onPressed: () async {
                      SystemUiStyle.setOrientation(Orientation.landscape);
                    },
                    style: TextButton.styleFrom(backgroundColor: Colors.amber, minimumSize: const Size(80, 40)),
                    child: const Text('固定横屏', style: TextStyle(color: Colors.white))),
                const SizedBox(width: 20),
                TextButton(
                    onPressed: () async {
                      SystemUiStyle.setOrientation(Orientation.portrait);
                    },
                    style: TextButton.styleFrom(backgroundColor: Colors.amber, minimumSize: const Size(80, 40)),
                    child: const Text('固定竖屏', style: TextStyle(color: Colors.white))),
              ]),
              const SizedBox(height: 20),
              TextButton(
                  onPressed: () async {
                    SystemUiStyle.setOrientation();
                  },
                  style: TextButton.styleFrom(backgroundColor: Colors.amber, minimumSize: const Size(140, 40)),
                  child: const Text('自动跟随设备方向', style: TextStyle(color: Colors.white))),
            ],
          ),
        ));
  }

  Future<String> contentData() async {
    String content = await rootBundle.loadString('assets/content.json');
    var json = jsonDecode(content);
    return json["content"];
  }
}
